<?php


namespace App\Admin\Controllers\Chart;


use App\Models\Order;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Rap2hpoutre\FastExcel\Facades\FastExcel;

class OrderChartController
{
	public function index(Content $content)
    {
		
		$data = [
			'oneDay' => $this->Chart('day'),
			'oneMonth' => $this->Chart('month'),
			'oneYear' => $this->Chart('year')
		];
		
        return $content->header('订单统计')
	        //->row(view('admin.export.export-btn', ['route' => route('orderExport')]))
	        ->row(function (Row $row) use ($data){
        
				    $row->column(11, function (Column $column) use($data) {
				    	$column->row(new Box('今日', view('admin.chart.order-chart', ['name' => 'oneDay', 'x' => $data['oneDay']['x'], 'data' => $data['oneDay']['data']])));
				    	$column->row(new Box('本月', view('admin.chart.order-chart', ['name' => 'oneMonth', 'x' => $data['oneMonth']['x'], 'data' => $data['oneMonth']['data']])));
				        $column->row(new Box('今年', view('admin.chart.order-chart', ['name' => 'oneYear', 'x' => $data['oneYear']['x'], 'data' => $data['oneYear']['data']])));
				    });
				});
    }
    
    public function Chart($index = 'year')
    {
        $timeStart = new Carbon();
    	$timeEnd = new Carbon();
    	$timeBetween = [];
	    switch ($index) {
	    	case 'day':
	    		$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
	    		$raw = 'Hour';
	    		$x = 24;
	    		break;
	    	case 'month':
	    		$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
	    		$raw = 'Day';
	    		$x = $timeStart->daysInMonth;
	    		break;
	    	case 'year':
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		$raw = 'Month';
	    		$x = 12;
	    		break;
	    	
	    	default:
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    }
	    
	    $data = Order::whereBetween('created_at', $timeBetween)
		    ->selectRaw($raw.'(created_at) as x,COUNT(*) as value')
		    ->groupBy('x')
		    ->get();
    	
		$x = countX($x);

		$data = fixMonth($x, $data->toArray());
		
		return $res = [
			'x' => $x,
			'data' => $data
		];
	   
    }
    // 导出
    public function export(Request $request)
    {
    	$time = new Carbon();
    	switch($request->get('date'))
	    {
		    case 'year':
		    	return FastExcel::data($this->exportData('year'))
	        ->download('订单.'.$time->format('Y').'年.xlsx');
		    	break;
		    case 'month':
		    	return FastExcel::data($this->exportData('month'))
	            ->download('订单.'.$time->format('Y-M').'月.xlsx');
		    	break;
		    case 'day':
		    	return FastExcel::data($this->exportData('day'))
	            ->download('订单.'.$time->format('Y-m-d').'.xlsx');
		    	break;
	    }
	    
    }
    
    public function exportData($index = 'year')
    {
    	$timeStart = new Carbon();
    	$timeEnd = new Carbon();
    	$timeBetween = [];
	    switch ($index) {
	    	case 'day':
	    		$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
	    		$raw = 'Hour';
	    		$x = 24;
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw($raw.'(created_at) as 小时,COUNT(*) as 数量')
			    ->groupBy('小时')
			    ->get();
	    		break;
	    	case 'month':
	    		$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
	    		$raw = 'Day';
	    		$x = $timeStart->daysInMonth;
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw($raw.'(created_at) as 日期,COUNT(*) as 数量')
			    ->groupBy('日期')
			    ->get();
	    		break;
	    	case 'year':
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		$raw = 'Month';
	    		$x = 12;
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw($raw.'(created_at) as 月份,COUNT(*) as 数量')
			    ->groupBy('月份')
			    ->get();
	    		break;
	    	
	    	default:
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    }
	    
	    
	    
	    return $data;
    }
}